<?php

function fetch_note($key) {
	global $pineza;
	set_pineza();

	$row = array();
	if (!$pineza->db) {
		$row['_error'] = 'no database connection';
		return($row);
	}

	$query = "SELECT * FROM `note` WHERE `key` = " .
		escape($key) . " LIMIT 1";
	$result = mysqli_query($pineza->db, $query);
	if (!$result) {
		$row['_error'] = 'SQL query failed (' . $query . ')';
		return($row);
	}

	while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
		mysqli_free_result($result);
		$row['_error'] = FALSE;
		return($row);
	}

	$row['_error'] = $key . ': note not found';
	return($row);
}

function new_note($login, $board, $text, $font = NULL, $color = NULL,
	$date = FALSE, $priority = 'NORMAL') {
	global $pineza;

	$font = ($font == NULL ? 'NULL' : "'${font}'");
	$color = ($color == NULL ? 'NULL' : "'${color}'");
	if (!$date) {
		$date_col = '';
		$date_val = '';
	}
	else {
		$date_col = ', `date`';
		$date_val = ", {$date}";
	}

	$query = "INSERT INTO `note` (`user`, `board`, `text`, `font`, `color`" .
		$date_col .  ", `priority`) VALUES ('" . escape($login) . "', '" .
		escape($board) . "', '" . escape($text) . "', {$font}, {$color}" .
		$date_val . ", '{$priority}')";
	if (!mysqli_query($pineza->db, $query))
		akiro($query . ': ' . mysqli_error($pineza->db));
	if (mysqli_affected_rows($pineza->db) != 1)
		akiro($query . ': insert failed');
	return(TRUE);
}

function print_board($login = FALSE, $board = FALSE) {
	global $pineza;

	if (!$login) {
		$board = FALSE;
	}

	if (!$board) {
		$board = '';
	}

	?>
	<div class="pinakas">
	<?php
	$result = mysqli_query($pineza->db,
		"SELECT * FROM `note` WHERE `user` = '" .
		escape($pineza->login) . "' AND `board` = '" . escape($board) . "' " .
		"ORDER BY `priority` DESC, `key` DESC");
	if (!$result)
		telos(mysqli_error($pineza->db));

	$not_empty = FALSE;
	while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
		print_note($row);
		$not_empty = TRUE;
	}
	?>
	</div>
	<div class="noFloat"></div>
	<?php
	if ($not_empty) {
		?>
		<div style="height: 10.0cm; font-size: 0.4cm; cursor: pointer;">
			Double click in this area, or click the note icon
			(<img src="<?php print $pineza->server; ?>images/note.gif"
				style="width: 0.7cm; margin-bottom: -0.2cm;"
				alt="new note icon" />)
			at the tool bar, for new post.
		</div>
		<?php
	}
}

function print_note($note) {
	global $pineza;
	?>
	<div id="n<?php print $note['key']; ?>" class="simioma">
		<table>

		<tr>
			<td colspan="2" class="tbldbg" style="height: 1.0em;">
			</td>
		</tr>

		<tr>
			<td class="tbldbg">
				<div style="background-color: #<?php
					print ($note['color'] ?  $note['color'] : ACHROMO); ?>;">
					<div id="s<?php print $note['key']; ?>"
						style="border-style: solid; border-width: 1px;
							border-color: <?php
							switch ($note['priority']) {
							case 'NORMAL':
								print '#999966';
								break;
							default:
								print 'red';
								break;
							}?>;">
						<?php kirios_simioma($note); ?>
					</div>
				</div>
			</td>

			<td class="tbldbg" style="width: 1.0em;">
			</td>
		</tr>

		<tr>
			<td colspan="2" class="tbldbg" style="height: 1.0em;">
			</td>
		</tr>

	</table>
	</div>
	<?php
}

function kirios_simioma($note) {
	global $pineza;
	?>
	<table>
	<tr>
	<td colspan="3" class="piastro panoPiastro tbldbg">
		<table width="100%" class="tbldbg">
		<tr>
		<td width="47%" class="tbldbg"
			onmouseover="showNTBL('<?php print $note['key']; ?>');"
			onmouseout="hideNTBL('<?php print $note['key']; ?>');">
			<?php note_tools_left($note); ?>
		</td>
		<?php // Η στήλη που ακολουθεί πρέπει να υπάρχει για λόγους εμφάνισης // ?>
		<td class="tbldbg" style="height: 24px;">
			&nbsp;
		</td>
		<td class="tbldbg" width="2%">
			<img class="panoPin" alt="pin" title="<?php
				print "Click to move that note. After select all notes " .
					"to be moved, click board to complete notes' " .
					"move."; ?>" src="<?php
				print $pineza->server; ?>images/yellowPin.png"
				onmouseup="pickNote(event, <?php print $note['key']; ?>)"
			/>
		</td>
		<?php // Η στήλη που ακολουθεί πρέπει να υπάρχει για λόγους εμφάνισης // ?>
		<td class="tbldbg" style="height: 24px;">
			&nbsp;
		</td>
		<td class="tbldbg" width="47%"
			onmouseover="getelid('ntbr<?php print $note['key']; ?>').style.display='table';"
			onmouseout="getelid('ntbr<?php print $note['key']; ?>').style.display='none';">
			<?php note_tools_right($note); ?>
		</td>
		</tr>
		</table>
	</td>
	</tr>

	<tr>
	<td class="piastro aristeroPiastro tbldbg">
	</td>

	<td class="tbldbg">
		<div id="st<?php print $note['key']; ?>" style="display: none;"></div>
		<div id="k<?php print $note['key']; ?>" class="soma<?php
			switch ($note['size']) {
			case 'WIDE':
				print ' somaWide';
				$help = 'Click to enlarge farther';
				break;
			case 'BIG':
				print ' somaBig';
				$help = 'Click to restore normal size';
				break;
			default:
				$help = 'Click to enlarge';
				break;
			}?>" style="font-family: <?php
			print ($note['font'] ? $note['font'] : 'serif') . ';';
			if ($note['width']) {
				print "min-width: {$note['width']}px;" .
					"max-width: {$note['width']}px;" .
					"width: {$note['width']}px;";
				$help = 'Click to restore normal size';
			}
			if ($note['height']) {
				print "min-height: {$note['height']}px;" .
					"max-height: {$note['height']}px;" .
					"height: {$note['height']}px;";
				$help = 'Click to restore normal size';
			}
			?>"
			title="<?php print $help; ?>, double click to edit"
			onclick="clickSimioma(event, <?php print $note['key']; ?>);"
			ondblclick="return editSimioma(event, <?php
				print $note['key']; ?>);">
			<?php print note_HTML($note['key'], $note['text']); ?>
		</div>
		<div id="XY<?php print $note['key']; ?>" style="float: right;"></div>
	</td>

	<td class="piastro dexiPiastro tbldbg"
		onmousedown="resizeStart(event, <?php print $note['key']; ?>, true, false);">
	</td>
	</tr>

	<tr>
	<td colspan="3" class="piastro katoPiastro tbldbg">
		<table width="100%"><tr>
			<td class="tbldbg" style="cursor: s-resize;"
				onmousedown="resizeStart(event, <?php
					print $note['key']; ?>, false, true);">
				<div class="noteDate"><?php
					print say_date($note['date']); ?></div>
			</td>
			<td class="cornerResize tbldbg" style="cursor: se-resize;"
				onmousedown="resizeStart(event, <?php
					print $note['key']; ?>, true, true);">
			</td>
		</tr></table>
	</td>

	</tr>
	</table>
	<?php
}

function note_HTML($key, $text) {
	$html = mb_ereg_replace('<a "', '<a target="_blank" ' .
		'onclick="getelid(\'s' . $key . "').justRestored=true; " .
		'return true;" href="', $text);
	$html = mb_ereg_replace('h>', 'h1>', $html);
	$html = mb_ereg_replace('\n', '<br />', $html);
	return $html;
}

function say_date($ymdhms) {
	$minas = array(
		'01' => 'Jan',
		'02' => 'Feb',
		'03' => 'Mar',
		'04' => 'Apr',
		'05' => 'May',
		'06' => 'Jun',
		'07' => 'Jul',
		'08' => 'Aug',
		'09' => 'Sep',
		'10' => 'Oct',
		'11' => 'Nov',
		'12' => 'Dec',
	);

	$x = explode(' ', $ymdhms);
	if (count($x) != 2)
		return('');

	$ymd = explode('-', $x[0]);
	if ((count($ymd) != 3) || (!array_key_exists($ymd[1], $minas)))
		return('');

	$hms = explode(':', $x[1]);
	if (count($hms) != 3)
		return('');

	return("{$minas[$ymd[1]]} {$ymd[2]}, {$ymd[0]}, {$hms[0]}:{$hms[1]}");
}

function note_tools_left($note) {
	global $pineza;
	switch ($note['priority']) {
	case 'NORMAL':
		$display = 'none';
		break;
	default:
		$display = 'table';
		break;
	}
	?>
	<table id="ntbl<?php print $note['key']; ?>" width="100%" class="tbldbg"
		style="display: <?php print $display; ?>;">
	<tr>
		<td class="noteToolLeft">
			<img class="noteToolIcon" src="<?php
				print $pineza->server; ?>images/important.png"
				title="Switch priority"
				onclick="importantNote('<?php print $note['key']; ?>');"
				alt="important icon" />
		</td>
	</tr>
	</table>
	<?php
}

function note_tools_right($note) {
	global $pineza;
	?>
	<table id="ntbr<?php print $note['key']; ?>" width="100%" class="tbldbg"
		style="display: none; margin-top: -2px;">
	<tr>
		<td class="noteToolRight tbldbg">
			<img class="noteToolIcon" style="margin-left: 0.4cm;" src="<?php
				print $pineza->server; ?>images/mailNote.png"
				title="Delete after mailing that note"
				onclick="mailNote('<?php print $note['key']; ?>', this);"
				alt="mail icon" />
		</td>
		<td class="noteToolRight tbldbg">
			<div style="float: right;">
				<img class="noteToolIcon" src="<?php
					print $pineza->server; ?>images/delete.png"
					title="Delete note"
					onclick="deleteNote('<?php print $note['key']; ?>');"
					alt="delete icon" />
			</div>
		</td>
	</tr>
	</table>
	<?php
}

function insert_board($login, $color, $title) {
	global $pineza;
	$query = "INSERT INTO `board` (`user`, `color`, `title`) " .
		"VALUES ('" . escape($login) . "', '{$color}', '" .
		escape($title) . "')";
	$result = mysqli_query($pineza->db, $query);
	if ((!$result) || (mysqli_affected_rows($pineza->db) != 1))
		telos('insert_board: ' . mysqli_error($pineza->db));
}

function forma_note($board = '', $note = FALSE) {
	global $pineza;
	set_pineza();

	if ($note) {
		$note_param = '&note=' . $note['key'];
		$titlos = 'Edit Note';
		$bgcolor = $note['color'];
	}
	else {
		$note_param = '';
		$titlos = 'New Note';
		$bgcolor = ZACHARI;
	}

	?>
	<form id="formaNote" name="note" method="post"
		action="<?php print $pineza->server; ?>board/index.php?neo=yes<?php
			print $note_param; ?>&board=<?php print $board; ?>"
		style="float: none; clear: both; background-color: #<?php
			print $bgcolor; ?>; width: 24.4cm; border-style: solid; border-width: 1px;">
	<table>
	<tr>
		<td class="tbldbg" style="min-width: 10.5cm; width: 10.5cm;">
			<div class="formaHeader">
				<span class="formaHeaderText" style="border-style: none;">
					<?php print $titlos; ?>
				</span>
			</div>
		</td>
		<td style="min-width: 1.2cm; width: 1.2cm;">
		</td>
		<td style="min-width: 12.0cm; width: 12.0cm;">
		</td>
	</tr>

	<tr>
		<td class="tbldbg">
			<textarea name="kimeno" class="kimeno"><?php
				if ($note) {
					print $note['text'];
				} ?></textarea>
		</td>
		<td class="tbldbg" style="vertical-align: top;">
			<?php
			paleta_xroma(PRASINO);
			paleta_xroma(KITRINO);
			paleta_xroma(VIOLETI);
			paleta_xroma(ZACHARI);
			paleta_xroma(GALAZIO);
			paleta_xroma(PORTOKALI);
			?>
		</td>
		<td class="tbldbg" style="vertical-align: top;">
			<?php
			paleta_font('Comic Sans MS, cursive, sans-serif');
			paleta_font('Tahoma, Geneva, sans-serif');
			paleta_font('Lucida Sans Unicode, Lucida Grande, sans-serif');
			paleta_font('Courier New, monospace');
			paleta_font('Georgia, serif');
			paleta_font('Times New Roman, Times, serif');
			?>
		</td>
	</tr>

	<tr>
		<td class="tbldbg">
			<div style="margin-left: 0.2em; margin-bottom: 2.0em;">
				<input type="submit" value="Save" class="fButton"
					onclick="this.form.localTime.value=currentTimestamp(true);" />
				<input type="submit" value="Reset" class="fButton"
					onclick="return(resetKimeno(this.form, '<?php
						if ($note) print $note['key']; ?>'));" />
				<input type="submit" value="Cancel" class="fButton"
					onclick="return(cancelKimeno(this.form, '<?php
						if ($note) print $note['key']; ?>'));" />
				<input name="color" type="hidden" value="<?php
					print ($note ? $note['color'] : ZACHARI); ?>" />
				<input name="font" type="hidden" value="<?php
					if ($note) print $note['font']; ?>" />
				<input name="localTime" type="hidden" value="" />
			</div>
		</td>
		<td class="tbldbg" colspan="2">
			<div class="odigies">
				Specify <a href="#" onclick="return setTitle();">titles</a>
				between <b>&lt;h&gt;</b> and <b>&lt;/h&gt;</b>, e.g.
				<b>&lt;h&gt;</b>Salary<b>&lt;/h&gt;</b><br />
				Insert links: <b>&lt;a "<span style="font-weight: normal;
				font-family: Courier New, sans-serif; font-style:
				italic;">url</span>"&gt;</b>text<b>&lt;/a&gt;</b>, e.g.<br />
				<b>&lt;a "<span style="font-weight: normal; font-family:
				Courier New, sans-serif;">www.pineza.wordpress.com</span>"&gt;</b><a
				target="_blank" href="http://www.pineza.wordpress.com">Pineza
				Blog</a><b>&lt;a/&gt;</b>
			</div>
		</td>
	</tr>
	</table>
	</form>
	<?php
}

function paleta_xroma($color) {
	?>
	<div onclick="setColor('<?php print $color; ?>', this);" class="paletaXroma"
		title="Click to color and save that note"
		style="background-color: #<?php print $color; ?>;"></div>
	<?php
}

function paleta_font($font) {
	?>
	<div onclick="setFont('<?php print $font; ?>');" class="paletaFont"
		title="Click to select that font"
		style="font-family: <?php print $font; ?>;"><?php print $font; ?></div>
	<?php
}

?>
